package cn.jsxz.common.utils;

import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 特殊表情处理util
 *
 * @author zy
 * @ClassName: EmojiUtil
 * @Description: TODO
 * @date 2018年7月24日
 */
public class EmojiUtil {

    /**
     * 去除emoji表情
     *
     * @param @param  source
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: deleteEmoji
     * @Description: TODO
     */
    public static String deleteEmoji(String source) {
        // 去掉emoji
        String pattern = "(?:[\uD83C\uDF00-\uD83D\uDDFF]|[\uD83E\uDD00-\uD83E\uDDFF]|[\uD83D\uDE00-\uD83D\uDE4F]|[\uD83D\uDE80-\uD83D\uDEFF]|[\u2600-\u26FF]\uFE0F?|[\u2700-\u27BF]\uFE0F?|\u24C2\uFE0F?|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?|[\u2934\u2935]\uFE0F?|[\u3030\u303D]\uFE0F?|[\u3297\u3299]\uFE0F?|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?|[\u203C\u2049]\uFE0F?|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?|[\u00A9\u00AE]\uFE0F?|[\u2122\u2139]\uFE0F?|\uD83C\uDC04\uFE0F?|\uD83C\uDCCF\uFE0F?|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?)";
        String reStr = "";
        Pattern emoji = Pattern.compile(pattern);
        Matcher emojiMatcher = emoji.matcher(source);
        String s = emojiMatcher.replaceAll(reStr);
        return s;
    }

    /**
     * 将str中的emoji表情转为byte数组
     *
     * @param @param  str
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: resolveToByteFromEmoji
     * @Description: TODO
     */
    public static String changeToByte(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        StringBuffer sb2 = new StringBuffer();

        try {
            str = new String(str.getBytes("UTF-8"), "UTF-8");

            Pattern pattern = Pattern
                    .compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]");
            Matcher matcher = pattern.matcher(str);
            while (matcher.find()) {
                matcher.appendReplacement(sb2, resolveToByte(matcher.group(0)));
            }
            matcher.appendTail(sb2);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.print(e);
        }
        return sb2.toString();
    }

    /**
     * 将str中的byte数组类型的emoji表情转为正常显示的emoji表情
     *
     * @param @param  obj
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: resolveToEmojiFromByte
     * @Description: TODO
     */
    public static String changeToEmoji(Object obj) {
        if (StringUtils.isEmpty(obj)) {
            return null;
        }
        String str = obj.toString();
        Pattern pattern2 = Pattern.compile("<:([[-]\\d*[,]]+):>");
        Matcher matcher2 = pattern2.matcher(str);
        StringBuffer sb3 = new StringBuffer();
        while (matcher2.find()) {
            matcher2.appendReplacement(sb3, resolveToEmoji(matcher2.group(0)));
        }
        matcher2.appendTail(sb3);
        String sb = sb3.toString();
        return sb;
    }

    //测试
    public static void main(String[] args) {
        System.out.println(deleteEmoji("🤔"));
        String str = changeToByte("👭ヾ🤔");//byte存贮
        System.out.println(changeToEmoji(str));//取表情
    }


    private static String resolveToByte(String str) {
        byte[] b;
        StringBuffer sb = new StringBuffer();
        try {
            b = str.getBytes("utf-8");
            sb.append("<:");
            for (int i = 0; i < b.length; i++) {
                if (i < b.length - 1) {
                    sb.append(Byte.valueOf(b[i]).toString() + ",");
                } else {
                    sb.append(Byte.valueOf(b[i]).toString());
                }
            }
            sb.append(":>");
        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();
        }
        return sb.toString();
    }

    private static String resolveToEmoji(String str) {
        str = str.replaceAll("<:", "").replaceAll(":>", "");
        String[] s = str.split(",");
        byte[] b = new byte[s.length];
        for (int i = 0; i < s.length; i++) {
            b[i] = Byte.valueOf(s[i]);
        }
        String sb = "";
        try {
            sb = new String(b, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return sb;
    }
}
